package com.sczmgk.hotline.mainhomeview;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sczmgk.hotline.R;
import com.sczmgk.hotline.activity.ChatProActivity;
import com.sczmgk.hotline.base.MainHomeBaseView;
import com.sczmgk.hotline.db.impl.IndicatorTitleDaoImpl;
import com.sczmgk.hotline.entity.IndicatorHomeInfo;
import com.sczmgk.hotline.httpUtils.MyHttp;
import com.sczmgk.hotline.mainhomeview.mainhomehome.MainHomeHomePage;
import com.sczmgk.hotline.utils.CommonUtils;
import com.sczmgk.hotline.utils.IndicatorUtils;
import com.sczmgk.hotline.view.MyViewPager;
import com.shizhefei.view.indicator.Indicator;
import com.shizhefei.view.indicator.ScrollIndicatorView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/2/26.
 */
public class MainHomeHome extends MainHomeBaseView {

    /*搜索图标*/
    private ImageView mIvSearch;

    /*指示器*/
    private ScrollIndicatorView mIndicator;

    /*指示器标题的集合*/
    private List<IndicatorHomeInfo> indicatorInfos;

    /*viewpager*/
    private MyViewPager mViewPage;

    /*界面的集合*/
    private List<MainHomeBaseView> lists;

    /*页面的viewpager*/
    private MyViewPageAdapter myPagerAdapter;

    public MainHomeHome(Activity context) {
        super(context);
        initTitle();
        initView();
        initData();
        setListener();
    }

    /**
     * 初始化标题
     */
    private void initTitle() {
        setHeadViewVisibility(View.GONE);
    }

    @Override
    public String getUrl() {
        return null;
    }

    /**
     * 初始化View
     */
    @Override
    public void initView() {

        LayoutInflater inflater = LayoutInflater.from(activity);
        View homeView = inflater.inflate(R.layout.main_home_home, null);

        mIvSearch = (ImageView) homeView.findViewById(R.id.iv_main_home_home_search);
        mIndicator = (ScrollIndicatorView) homeView.findViewById(R.id.siv_main_home_home_indicator);
        mViewPage = (MyViewPager) homeView.findViewById(R.id.vp_main_home_home_page);
        TextView mTvAppName = (TextView) homeView.findViewById(R.id.tv_main_home_home_appname);
        mTvAppName.setText(CommonUtils.getAppName(activity));

        container.addView(homeView);//返回当前的View
    }

    /**
     * 初始化数据
     */
    private void initData() {

        //从数据库获取指示器标题
        indicatorInfos = IndicatorTitleDaoImpl.getIntance(activity).findAll();

        //设置Indicator指示器
        mIndicator.setBackgroundResource(R.color.white);
        String[] indicatorTitle = getIndicatorTitles();
        CommonUtils.setIndector(mIndicator, indicatorTitle, activity);

        //添加页面
        lists = new ArrayList<>();
        if (indicatorInfos != null) {
            initViewPagerItem();
        }
    }

    private void initViewPagerItem() {

        if (lists.size() > 0){
            lists.clear();
        }

        for (int i = 0; i < indicatorInfos.size(); i++) {
            IndicatorHomeInfo info = indicatorInfos.get(i);
            MainHomeHomePage homePage = new MainHomeHomePage(activity,info.articleid);
            lists.add(homePage);
        }

        //设置viewpager的adapter
        myPagerAdapter = new MyViewPageAdapter();
        mViewPage.setAdapter(myPagerAdapter);
    }

    private class MyViewPageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            MainHomeBaseView item = lists.get(position);
            View view = item.getView();
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            MainHomeBaseView item = lists.get(position);
            View view = item.getView();
            container.removeView(view);
        }
    }


    /**
     * 设置指示器标题
     */
    public String[] getIndicatorTitles() {
        if (indicatorInfos == null || indicatorInfos.size() == 0) {
            return new String[0];
        }
        String[] indicatorTitle = new String[indicatorInfos.size()];
        for (int i = 0; i < indicatorInfos.size(); i++) {
            IndicatorHomeInfo indicator = indicatorInfos.get(i);
            indicatorTitle[i] = indicator.title;
        }
        return indicatorTitle;
    }

    /**
     * 设置监听
     */
    private void setListener() {
        mIndicator.setOnItemSelectListener(new Indicator.OnItemSelectedListener() {
            @Override
            public void onItemSelected(View view, int position, int i1) {
                MyHttp.cancle();
                mViewPage.setCurrentItem(position);
                for (int i = 0; i < lists.size(); i++) {
                    MainHomeBaseView homeView = lists.get(i);
                    if (i == position){
                        homeView.onResume();
                    }else{
                        homeView.onPause();
                    }
                }
            }
        });

        mIvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.startActivity(new Intent(activity, ChatProActivity.class));
            }
        });

        mViewPage.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mIndicator.setCurrentItem(position);
            }
        });
    }

    @Override
    public void mHandleMessage(Message msg) {


    }

    @Override
    public void onResume() {
        super.onResume();

        if (indicatorInfos != null && indicatorInfos.size() > 0) {
            int currentItem = mIndicator.getCurrentItem();
            for (int i = 0; i < lists.size(); i++) {
                MainHomeBaseView mainHomeBaseView = lists.get(i);
                if (i == currentItem){
                    mainHomeBaseView.onResume();
                }else{
                    mainHomeBaseView.onPause();
                }
            }
        } else {
            //加载指示器标题
            IndicatorUtils.getIndicator(activity, new IndicatorUtils.OnLoadDataFinish() {
                @Override
                public void onSuccess() {
                    //获取数据库中的内容
                    indicatorInfos = IndicatorTitleDaoImpl.getIntance(activity).findAll();
                    if (indicatorInfos != null && indicatorInfos.size() > 0) {
                        //设置指示器内容
                        String[] indicatorTitle = getIndicatorTitles();
                        CommonUtils.setIndector(mIndicator, indicatorTitle, activity);

                        initViewPagerItem();
                    }
                }

                @Override
                public void onError(String msg) {

                }
            });
        }
    }

}
